package com.iseastar.guojiang.team;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.team.fragment.CreatingTeamFragment;
import com.shouhuobao.station.R;

import droid.frame.fragmentation.SupportFragment;
import droid.frame.utils.Android;

/**
 * @author 李澄锋<br>
 * 2018/6/12
 * 战队中心列表 分为 运营中战队 和 组队中战队
 */
public class TeamCenterZoneListActivity extends BaseActivity2 implements View.OnClickListener {

    private SupportFragment[] mFragments = new SupportFragment[2];
    private TextView createdTeamTV;
    private TextView creatingTeamTV;
    int position = 0;
    int prePosition = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (savedInstanceState == null) {
//            mFragments[0] = new OperationTeamFragment();
//            mFragments[1] = new CreatingTeamFragment();
//            loadMultipleRootFragment(R.id.fl_container, 0, mFragments);
//        } else {
//            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
//
//            // 这里我们需要拿到mFragments的引用,也可以通过getSupportFragmentManager.getFragments()自行进行判断查找(效率更高些),用下面的方法查找更方便些
//            mFragments[0] = findFragment(OperationTeamFragment.class);
//            mFragments[1] = findFragment(CreatingTeamFragment.class);
//        }
//        showHideFragment(mFragments[position], mFragments[prePosition]);
        setFragmentIndicator(position);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_team_center_zone_list);
        super.findViewById();
        getAppTitle().setCommonTitle("战队中心");
        //运营中战队
        createdTeamTV = findViewById(R.id.created_team_tv);
        createdTeamTV.setOnClickListener(this);
        //组队中战队
        creatingTeamTV = findViewById(R.id.creating_team_tv);
        creatingTeamTV.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //运营中
            case R.id.created_team_tv:
                prePosition = position;
                position = 0;
                if (position == prePosition) {
                    return;
                }
                createdTeamTV.setBackgroundResource(R.drawable.courier_all_team_left_checked_bg);
                creatingTeamTV.setBackgroundColor(getResources().getColor(R.color.transparent));
                createdTeamTV.setTextColor(getResources().getColor(R.color.white));
                creatingTeamTV.setTextColor(getResources().getColor(R.color.app_theme_color1));
                showHideFragment(mFragments[position], mFragments[prePosition]);
                break;
            //组队中
            case R.id.creating_team_tv:
                prePosition = position;
                position = 1;
                if (position == prePosition) {
                    return;
                }
                ((CreatingTeamFragment) mFragments[1]).refreshData();
                createdTeamTV.setBackgroundColor(getResources().getColor(R.color.transparent));
                creatingTeamTV.setBackgroundResource(R.drawable.courier_all_team_right_checked_bg);
                createdTeamTV.setTextColor(getResources().getColor(R.color.app_theme_color1));
                creatingTeamTV.setTextColor(getResources().getColor(R.color.white));
                showHideFragment(mFragments[position], mFragments[prePosition]);
                break;
        }
    }

    private void setFragmentIndicator(int whichIsDefault) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        mFragments[0] = (SupportFragment) fragmentManager
                .findFragmentById(R.id.fragment_operate);
        mFragments[1] = (SupportFragment) fragmentManager
                .findFragmentById(R.id.fragment_creating);
        fragmentManager.beginTransaction().hide(mFragments[0])
                .hide(mFragments[1]).show(mFragments[whichIsDefault]).commit();
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //申请加入战队成功
            case MsgID.courier_team_apply_team:
                cancelLoadingDialog();
                getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Android.hideSoftInput(getContext());
                    }
                }, 200);
                ReqResult<?> reqResult2 = JSON.parser(msg.obj);
                if (checkLoginStatus(reqResult2)) {
                    showToast("申请成功！请等待战队负责人审核");
                } else {
                    showToast(reqResult2.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }
}
